<template>
  <div class="app-container">
    <el-row :gutter="20">
      <!--商户数据-->
      <el-col :span="20" :xs="24">

        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" label-width="68px">
          <el-form-item label="负责人名称" prop="userName">
            <el-input
              v-model="queryParams.userName"
              placeholder="请输入用户名称"
              clearable
              style="width: 240px"
              @keyup.enter.native="handleQuery"/>
          </el-form-item>

          <el-form-item label="手机号码" prop="phonenumber">
            <el-input
              v-model="queryParams.phonenumber"
              placeholder="请输入手机号码"
              clearable
              style="width: 240px"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>

          <el-form-item label="商户号" prop="phonenumber">
            <el-input
              v-model="queryParams.merchantCode"
              placeholder="请输入商户号"
              clearable
              style="width: 240px"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="企业名称" prop="phonenumber">
            <el-input
              v-model="queryParams.comName"
              placeholder="请输入商户号"
              clearable
              style="width: 240px"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>

          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="reset">重置</el-button>
          </el-form-item>
        </el-form>


        <el-table v-loading="loading" :data="recordsList">
          <el-table-column label="负责人编号" align="center" prop="userId"/>
          <el-table-column label="账号" align="center" prop="userName"/>
          <el-table-column label="负责人" align="center" prop="nickName"/>
          <el-table-column label="商户号" align="center" prop="merchantCode"/>
          <el-table-column label="企业名" align="center" prop="comName"/>
          <el-table-column label="联系电话" align="center" prop="phonenumber"></el-table-column>
          <el-table-column label="余额" align="center" prop="balance"></el-table-column>
          <el-table-column label="操作" align="center">
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="text"
                icon="el-icon-edit"
                @click="handleUpdate(scope.row)"
                v-hasPermi="['system:user:edit']"
              >对其充值
              </el-button>
            </template>
          </el-table-column>
        </el-table>

        <pagination
          v-show="total>0"
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"

          @pagination="getList"
        />
      </el-col>
    </el-row>

    <!-- 添加或修改用户配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>

          <el-col>
            <el-form-item label="充值额度" prop="edu">
              <el-input v-model.number="form.edu" placeholder="输入额度" maxlength="11"/>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
  import {addMerchantYue, getAccounts} from "@/api/payOrder/payOrder";

  export default {

    data() {
      return {
        // 遮罩层
        loading: true,
        open: false,
        title: '',
        // 总条数
        total: 0,
        // 用户表格数据
        recordsList: null,
        // 选择
        xz: null,
        // 表单参数
        form: {
          edu: null
        },
        defaultProps: {
          children: "children",
          label: "label"
        },
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          userName: undefined,
          phonenumber: undefined,
          merchantCode: undefined,
          comName: undefined
        },

        // 表单校验
        rules: {
          edu: [
            {required: true, message: "请输入金额", trigger: "blur"},
            {type: "number", message: "输入充值额有误", trigger: 'blur'}
          ]
        }
      };
    },
    watch: {},
    created() {
      this.getList();
    },
    methods: {
      /** 拉取账户列表 */
      getList() {
        this.loading = true;
        getAccounts(this.queryParams).then(response => {
            this.recordsList = response.rows;
            this.total = response.total;
            this.loading = false;
          }
        );
      },

      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },

      // 表单重置
      reset() {
        // 查询参数
        this.queryParams = {
          userName: undefined,
          phonenumber: undefined,
          merchantCode: undefined,
          comName: undefined
        }
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },

      handleUpdate(item) {
        this.xz = item
        this.title = '对商户号：' + item.merchantCode + '（负责人 ' + item.nickName + '）充值'
        this.open = true
      },

      /** 修改提交按钮 */
      submitForm: function () {
        this.$refs["form"].validate(valid => {
          if (valid) {

            if (this.form.edu != null) {
              var form = {
                czUserId: this.$store.state.user.id,
                jsczUserId: this.xz.userId,
                edu: this.form.edu
              }
              addMerchantYue(form).then(response => {
                this.$modal.msgSuccess("充值成功");
                this.open = false;
                this.getList();
              });
            }
          } else {
          }
        });
      },

    }
  };
</script>
